package com.videoweb.service.impl;

import com.videoweb.constant.Constants;
import com.videoweb.context.PermissionContextHolder;
import com.videoweb.entity.Role;
import com.videoweb.entity.User;
import com.videoweb.model.LoginUser;
import com.videoweb.utils.SecurityUtils;
import com.videoweb.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户权限处理
 *
 * @author ruoyi
 */
@Component
@Service
public class PermissionService {

    @Autowired
    private MenuServiceImpl menuService;

    @Autowired
    private RoleServiceImpl roleService;





    /**
     * 验证用户是否具备某权限
     *
     * @param permission 权限字符串
     * @return 用户是否具备某权限
     */
    public boolean hasPermi(String permission)
    {
        if (StringUtils.isEmpty(permission))
        {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getPermissions()))
        {
            return false;
        }
        PermissionContextHolder.setContext(permission);
        return hasPermissions(loginUser.getPermissions(), permission);
    }
    private boolean hasPermissions(Set<String> permissions, String permission)
    {
        return permissions.contains(Constants.ALL_PERMISSION) || permissions.contains(StringUtils.trim(permission));
    }



    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(User user) {
        Set<String> roles = new HashSet<String>();


        roles.addAll(roleService.selectRolePermissionByUserId(user.getId()));

        return roles;
    }


    public Set<String> getMenuPermission(User user) {
        Set<String> perms = new HashSet<String>();
//        // 管理员拥有所有权限
//        if (user.isAdmin())
//        {
//            perms.add("*:*:*");
//        }
//        else
//        {

        List<Role> roles = user.getRoles();
//        if (!CollectionUtils.isEmpty(roles)) {
        // 多角色设置permissions属性，以便数据权限匹配权限
        for (Role role : roles) {

//   得到权限的集合 根据用户拥有的角色来给与权限
            Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getId());
//            Set<String> rolePerms = null;
            role.setPermissions(rolePerms);
            perms.addAll(rolePerms);
        }
//            也就是根据角色ID来给权限
//            for (Role role : roles)
//            {
//                if (StringUtils.equals(role.getStatus()))
//                {
////                        得到权限的集合
//                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
//                    role.setPermissions(rolePerms);
//                    perms.addAll(rolePerms);
//                }
//            }
////            }
//            else
//            {
//                perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
//            }
//        }
        return perms;

    }





}
